package top.frsf.tank;

import top.frsf.GamePanel;
import top.frsf.bullet.Bullet;
import top.frsf.bullet.EnemyBullet;
import top.frsf.tool.Direction;

import java.awt.*;
import java.util.Random;

/**
 * @BelongsProject: TankWar1
 * @BelongsPackage: top.frsf.tank
 * @Author: FRSF
 * @CreateTime: 2022-09-03  12:45
 * @Description: 人机tank类
 * @Version: 1.0
 */
public class Bot extends TankObj {

    // 统计移动次数，为随机方向做准备
    int moveTime = 0;


    public Bot(String img, int x, int y, GamePanel gamePanel,
               String upImage, String downImage, String leftImage, String rightImage) {
        super(img, x, y, gamePanel, upImage, downImage, leftImage, rightImage);
    }

    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img, x, y, null);
        go();
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x, y, width, height);
    }

    /* 电脑tank,随机方向生成,对应enum 0-3*/
    public Direction getRandomDirection() {
        Random random = new Random();
        Direction direction;
        int num = random.nextInt(4);
        switch (num) {
            case 0:
                direction = Direction.UP;
                break;
            case 1:
                direction = Direction.DOWN;
                break;
            case 2:
                direction = Direction.LEAF;
                break;
            default:
                direction = Direction.RIGHT;
        }
        return direction;
    }

    /* 电脑tank 攻击*/
    @Override
    public void attack() {
        Point p = getHeadPoint();
        Random random = new Random();
        int num = random.nextInt(100);
        if (num < 1) {
            this.gamePanel.bulletList.add(new EnemyBullet("src/main/resources/bullet/botbullet.png",
                    p.x - 7, p.y - 7, this.gamePanel, this.direction));
        }
    }

    public void go() {
        attack();
        if (moveTime >= 50) {
            direction = getRandomDirection();
            moveTime = 0;
        } else {
            moveTime++;
        }
        switch (direction) {
            case UP:
                upward();
                break;
            case DOWN:
                downward();
                break;
            case RIGHT:
                rightward();
                break;
            case LEAF:
                leftward();
                break;
        }
    }


}
